package cn.iocoder.yudao.module.wine.controller.app.supplyorder;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.module.wine.controller.app.supplyinfo.vo.AppCreateOrderReqVO;
import cn.iocoder.yudao.module.wine.controller.app.supplyorder.vo.*;
import cn.iocoder.yudao.module.wine.service.supplyorder.SupplyOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @author zyc
 * @createDate 2024/4/22 17:17
 */
@Tag(name = "用户 APP - 订单信息")
@RestController
@RequestMapping("/wine/supply-order")
@Validated
public class AppSupplyOrderController {

    @Resource
    private SupplyOrderService supplyOrderService;

    @GetMapping("/get")
    @Operation(summary = "获得订单详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppSupplyOrderRespVO> getOrderInfo(@RequestParam("id") Long id) {
        AppSupplyOrderRespVO supplyOrder = supplyOrderService.appGetSupplyOrder(id);
        supplyOrder.setBuyAreaName(AreaUtils.format(supplyOrder.getBuyAreaId(), " "));
        supplyOrder.setSaleAreaName(AreaUtils.format(supplyOrder.getSaleAreaId(), " "));
        return success(supplyOrder);
    }

    @GetMapping("/list")
    @Operation(summary = "获取我的订单列表")
    public CommonResult<List<AppSupplyOrderRespVO>> getOrderList(@Valid AppSupplyOrderListReqVO listReqVO) {
        List<AppSupplyOrderRespVO> list = supplyOrderService.selectOrderList(listReqVO);

        List<AppSupplyOrderRespVO> collect = list.stream().peek(item -> {
            item.setBuyAreaName(AreaUtils.format(item.getBuyAreaId(), " "));
            item.setSaleAreaName(AreaUtils.format(item.getSaleAreaId(), " "));
        }).collect(Collectors.toList());
        return success(collect);
    }

    /**
     * 卖家提交发货、买家提交退货、更新订单完成 使用
     * @param reqVO
     * @return
     */
    @PostMapping("/update")
    @Operation(summary = "订单信息修改")
    public CommonResult<Long> salesReturn(@Valid @RequestBody AppSupplyOrderUpdateReqVO reqVO) {
        return success(supplyOrderService.appUpdateSupplyOrder(reqVO));
    }

    @PostMapping("/sales-return")
    @Operation(summary = "买家申请退货")
    public CommonResult<Long> salesReturn(@Valid @RequestBody AppSalesReturnReqVO reqVO) {
        return success(supplyOrderService.salesReturn(reqVO));
    }

    @PostMapping("/cancel")
    @Operation(summary = "订单取消")
    public CommonResult<Long> cancel(@Valid @RequestBody AppCancelReqVO reqVO) {
        return success(supplyOrderService.cancel(reqVO));
    }

    @PostMapping("/sales-audit")
    @Operation(summary = "卖家同意或拒绝退货")
    public CommonResult<Long> salesAudit(@Valid @RequestBody AppSalesAuditReqVO reqVO) {
        return success(supplyOrderService.salesAudit(reqVO));
    }

    @PostMapping("/sales-confirm")
    @Operation(summary = "卖家确认收货")
    public CommonResult<Long> salesConfirm(@Valid @RequestBody AppSalesConfirmReqVO reqVO) {
        return success(supplyOrderService.salesConfirm(reqVO));
    }

    @PostMapping("/confirm-receipt")
    @Operation(summary = "买家确认收货")
    public CommonResult<Long> confirmReceipt(@Valid @RequestBody AppSalesReturnReqVO reqVO) {
        return success(supplyOrderService.confirmReceipt(reqVO));
    }

    @PostMapping("/affirm-duty")
    @Operation(summary = "供求信息订单确认担责")
    public CommonResult<Long> affirmDuty(@Valid @RequestBody AppAffirmDutyReqVO reqVO) {
        return success(supplyOrderService.affirmDuty(reqVO));
    }

    /**
     * 1.生成普通订单、担责订单
     * @param reqVO
     * @return
     */
    @PostMapping("/place-an-order")
    @Operation(summary = "供求信息下单（普通订单 担责订单）")
    public CommonResult<Boolean> placeAnOrder(@Valid @RequestBody AppCreateOrderReqVO reqVO) {
        return success(supplyOrderService.placeAnOrder(reqVO));
    }

}
